package ch.supsi.parentalcontrol;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

import ch.supsi.mobile.core.Address;
import ch.supsi.mobile.core.Page;

public class GestioneSchedaUser implements ActionListener {

	private UserPanel up;
	
	public GestioneSchedaUser(UserPanel ap) {
		this.up = ap;
	}

	@Override
	public void actionPerformed(ActionEvent evt) {
		testUserAction();
	}
	
	public boolean testUserAction(){
		Boolean usingWhiteListMethod = Grafica.getWhiteListRadio().isSelected();
		
		// nessun parametro di ricerca
		if(up.getTargetText().getText().equals("")){
			//JOptionPane.showMessageDialog(null,"Inserisci un valore","Error",JOptionPane.WARNING_MESSAGE);
			up.getResultMessage().setText("Result: inserisci un valore!");
			up.getResultMessage().setForeground(new Color(255, 0, 0));
			return false;
		}
		// cerca il valore nella lista
		else{
			String param = up.getTargetText().getText();
			Pattern pattern = Pattern.compile(param);
			Matcher matcher;
			String matcherParam = null;
			DefaultListModel modelToMatchWith;
			
			// filtro WHITE LIST
			if(usingWhiteListMethod)
				modelToMatchWith = up.getWhiteListModel();
			
			// filtro BLACK LIST
			else
				modelToMatchWith = up.getBlackListModel();
			
			for(int i=0; i<modelToMatchWith.size(); i++){
				// estraggo il valore da comparare
				if(up.getType().equals("address")){
					System.out.println(modelToMatchWith.get(i));
					Address add = (Address)modelToMatchWith.get(i);
					matcherParam = add.getNumber();
				}else if(up.getType().equals("browser")){
					Page add = (Page)modelToMatchWith.get(i);
					matcherParam = add.getURL();
				}
				
				matcher = pattern.matcher(matcherParam);
				while(matcher.find()){
					if(usingWhiteListMethod){
						//JOptionPane.showMessageDialog(null,"Permesso!","Good",JOptionPane.INFORMATION_MESSAGE);
						up.getResultMessage().setText("Result: permesso!");
						up.getResultMessage().setForeground(new Color(0, 255, 0));
			        	return true;
					}else{
						//JOptionPane.showMessageDialog(null,"Non permesso!","Error",JOptionPane.WARNING_MESSAGE);
						up.getResultMessage().setText("Result: non permesso!");
						up.getResultMessage().setForeground(new Color(255, 0, 0));
			        	return false;
					}
		        }
				matcher=null;
			}
			
		}

		if(usingWhiteListMethod){
			//JOptionPane.showMessageDialog(null,"Non permesso!","Error",JOptionPane.WARNING_MESSAGE);
			up.getResultMessage().setText("Result: non permesso!");
			up.getResultMessage().setForeground(new Color(255, 0, 0));
			return false;
		}else{
			//JOptionPane.showMessageDialog(null,"Permesso!","Good",JOptionPane.INFORMATION_MESSAGE);
			up.getResultMessage().setText("Result: permesso!");
			up.getResultMessage().setForeground(new Color(0, 255, 0));
			return true;
		}
		
	}
}
